package com.xneure.enging;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.xneure.code.log.ILog;
import com.xneure.code.log.ILog.Level;
import com.xneure.enging.event.EventNotify;
import com.xneure.utils.SpringUtil;

@Component
public class RequestInterceptor implements HandlerInterceptor{
	@Resource
	private ILog log;

	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
		log.print(Level.DEBUG,request.getRequestURL().toString());
		RequestContext.setParam(SpringUtil.parseRequestParam());
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,ModelAndView modelAndView) throws Exception{
	}

	@Override
	public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex) throws Exception{
		if(request.getSession(false) != null){
			EventNotify eventNotify = RequestContext.getEventNotify(request.getSession().getId());
			eventNotify.initRes();
		}
	}
}
